package com.tengju.user.infrastructure.repository.chief;

import com.tengju.user.domain.model.chief.ChiefSecCount;
import com.tengju.user.domain.model.chief.ChiefGenSecRelation;
import com.tengju.user.domain.model.chief.ChiefGenSecRelationRepository;
import com.tengju.user.domain.model.chief.ChiefId;
import com.tengju.user.domain.model.department.DepartmentId;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.domain.shared.IdObject;
import com.tengju.user.infrastructure.persistence.mybatis.dataobject.ChiefSecCountDO;
import com.tengju.user.infrastructure.persistence.mybatis.dataobject.ChiefGenSecRelationDO;
import com.tengju.user.infrastructure.persistence.mybatis.mapper.dealer.custom.ChiefGenSecRelationDOMapper;
import com.tengju.user.infrastructure.repository.convert.ChiefGenSecInfoRelationConvert;
import com.tengju.user.infrastructure.repository.convert.ChiefGenSecRelationConvert;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

@Repository
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class ChiefGenSecRelationRepositoryImpl implements ChiefGenSecRelationRepository {

    private final ChiefGenSecRelationDOMapper chiefGenSecRelationDOMapper;

    @Override
    public List<ChiefGenSecRelation> getChiefGenSecRelation(ChiefId chiefId) {
        List<ChiefGenSecRelationDO> chiefGenSecRelationDOList = chiefGenSecRelationDOMapper.queryByChiefId(chiefId.getValue());
        if (CollectionUtils.isEmpty(chiefGenSecRelationDOList)) {
            return Collections.emptyList();
        }
        return chiefGenSecRelationDOList.stream()
                .map(ChiefGenSecRelationConvert::convert)
                .collect(Collectors.toList());
    }

    @Override
    public void batchInsert(List<ChiefGenSecRelation> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        List<ChiefGenSecRelationDO> chiefGenSecRelationDOList = list.stream()
                .map(ChiefGenSecRelationConvert::convert)
                .collect(Collectors.toList());
        chiefGenSecRelationDOMapper.batchInsert(chiefGenSecRelationDOList);
    }

    @Override
    public int batchRemove(ChiefId chiefId) {
        return chiefGenSecRelationDOMapper.batchRemove(chiefId.getValue());
    }


    @Override
    public List<ChiefSecCount> queryByChiefIdAndSecIdPage(ChiefId chiefId, UserIdCode genSecId, Integer offset, Integer pageSize) {
        List<ChiefSecCountDO> chiefSecRelationDOList = chiefGenSecRelationDOMapper
                .queryByChiefIdAndSecId(Optional.ofNullable(chiefId).map(IdObject::getValue).orElse(null),
                        Optional.ofNullable(genSecId).map(IdObject::getValue).orElse(null),
                        offset, pageSize);
        return chiefSecRelationDOList.stream().map(ChiefGenSecInfoRelationConvert::convert).collect(Collectors.toList());
    }

    @Override
    public int count(ChiefId chiefId, UserIdCode genSecId) {
        return chiefGenSecRelationDOMapper.count(Optional.ofNullable(chiefId).map(IdObject::getValue).orElse(null),
                Optional.ofNullable(genSecId).map(IdObject::getValue).orElse(null));
    }


    @Override
    public List<UserIdCode> queryGenSecIdCodeList(ChiefId chiefId) {
        if (chiefId == null){
            return Collections.emptyList();
        }
        return chiefGenSecRelationDOMapper.queryGenSecIdCodeList(chiefId.getValue()).stream().map(UserIdCode::new).collect(Collectors.toList());
    }

    @Override
    public List<ChiefId> chiefIdList() {
        List<Long> chiefIdList = chiefGenSecRelationDOMapper.chiefIdList();
        return chiefIdList.stream().distinct().map(ChiefId::new).collect(Collectors.toList());
    }


    @Override
    public void updateChiefDepartmentRelation(ChiefGenSecRelation chiefSecRelation) {
        ChiefGenSecRelationDO relationDO = ChiefGenSecRelationConvert.convert(chiefSecRelation);
        chiefGenSecRelationDOMapper.updateChiefDepartmentRelation(relationDO);
    }

    @Override
    public List<ChiefGenSecRelation> getChiefGenSecRelationBySecsDepartments(List<UserIdCode> userIdCodeList, List<DepartmentId> departmentIds) {
        if (CollectionUtils.isEmpty(userIdCodeList)){
            return Collections.emptyList();
        }
        List<Long> secIdList = userIdCodeList.stream().map(UserIdCode::getValue).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(departmentIds)){
            departmentIds = new ArrayList<>();
        }
        List<Long> departmentIdList = departmentIds.stream().map(DepartmentId::getValue).collect(Collectors.toList());
        List<ChiefGenSecRelationDO> chiefGenSecRelationDOList = chiefGenSecRelationDOMapper.selectRelationsBySecsDepartments(secIdList, departmentIdList);
        return chiefGenSecRelationDOList.stream().map(ChiefGenSecRelationConvert :: convert).collect(Collectors.toList());
    }
}
